package com.taptech.doufu.ui.view.choice.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.choice.ComicModel;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.view.choice.ChoiceUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class ChoiceTopicComicGridAdapter extends RecyclerView.Adapter<ChoiceTopicComicGridAdapter.ViewHolder>{

    private List<ComicModel> list;
    private Context context;
    private int picWidth;
    private int picHeight;
    private EventBean eventData;
    public ChoiceTopicComicGridAdapter(Context context, List<ComicModel> list,EventBean eventData){
        this.list = list;
        this.context = context;
        this.eventData = eventData;
        picHeight = ChoiceUtils.getHeight(context);
        picWidth = ChoiceUtils.getPicWidth(context);
    }

    public ComicModel getItem(int position){
        if (list != null && list.size()>position) {
            return list.get(position);
        }
        return null;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.topic_choice_grid_item, null);
        ViewHolder holder = new ViewHolder(view);
        holder.tvTitle = view.findViewById(R.id.tvTitle);
        holder.ivPic = view.findViewById(R.id.ivPic);
        holder.lay = view.findViewById(R.id.lay);
        holder.layTag = view.findViewById(R.id.layTag);
        holder.tvTag1 = view.findViewById(R.id.tvTag1);
        holder.tvTag2 = view.findViewById(R.id.tvTag2);
        RelativeLayout.LayoutParams picParams = new RelativeLayout.LayoutParams(picWidth,picHeight);
        holder.ivPic.setLayoutParams(picParams);
        holder.tvTitle.setMaxWidth(picWidth);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final ComicModel model = getItem(position);
        if (model != null) {
            holder.tvTitle.setText(model.getTitle());
            holder.tvTitle.setMaxLines(2);

            if (!TextUtils.isEmpty(model.getImage_v())) {
                ChoiceUtils.displayImage(context,model.getImage_v(),holder.ivPic);

            }else if (!TextUtils.isEmpty(model.getImage())) {
                ChoiceUtils.displayImage(context,model.getImage(),holder.ivPic);
            }
            holder.layTag.setVisibility(View.GONE);
            if (model.getTag_list() != null) {
                String[] tags = model.getTag_list().split(" ");
                if (tags != null && tags.length > 0) {
                    holder.layTag.setVisibility(View.VISIBLE);
                    if (tags[0] != null) {
                        holder.tvTag1.setText((tags[0]));
                        holder.tvTag1.setVisibility(View.VISIBLE);
                    }else{
                        holder.tvTag1.setVisibility(View.GONE);
                    }
                    if ((tags.length > 1) && (tags[1] != null)) {
                        holder.tvTag2.setText((tags[1]));
                        holder.tvTag2.setVisibility(View.VISIBLE);
                    }else{
                        holder.tvTag2.setVisibility(View.GONE);
                    }
                }
            }
            holder.lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChoiceUtils.addEvent(context,UmengEventName.JINGXUAN_CARD_CONTENT,eventData);
                    SimpleWeexActivity.startActivity(context,"TFNovel/TFComicDetail.js?id="+model.getId());
                }
            });
            changeDayModelBg(holder);
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvTitle;
        ImageView ivPic;

        TextView tvTag1,tvTag2;
        View layTag;
        View lay;
        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder);
        }else {
            dayBg(viewHolder);
        }
    }

    private void dayBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.white));
        viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_22));
        viewHolder.ivPic.setAlpha(1.0f);
    }

    private void darkBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        viewHolder.ivPic.setAlpha(Constant.imagViewAlpha);
    }
}
